package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.ConsumMonitor;
import com.dy.nygl.pojo.EnergyYear;
import com.dy.nygl.service.ConsumMonitorService;
import com.dy.nygl.service.EnergyYearService;
import com.sun.java.swing.plaf.motif.resources.motif;

@Controller
@RequestMapping("/energyYear")
public class EnergyYearController {
	private String select = "jsp/a2.jsp";

	private List<EnergyYear> energyYearList;

	@Resource(name = "energyYearService")
	private EnergyYearService energyYearService;

	@Resource(name = "consumMonitorService")
	private ConsumMonitorService consumMonitorService;

	public List<EnergyYear> getEnergyYearList() {
		return energyYearList;
	}

	public void setEnergyYearList(List<EnergyYear> energyYearList) {
		this.energyYearList = energyYearList;
	}

	public String getSelect() {
		return select;
	}

	public void setSelect(String select) {
		this.select = select;
	}

	@RequestMapping("/synchronize.action")
	public void SynchronizeT() {

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date currTime = new java.util.Date();

		String curTime = formatter.format(currTime);

		System.err.println(curTime);
		String[] date1 = curTime.split("-");
		String month1 = date1[1];

		List<EnergyYear> list = energyYearService.selectAll();
		String elec = list.get(0).getYear();
		String water = list.get(1).getYear();
		String natural = list.get(2).getYear();
		String gas = list.get(3).getYear();
		String oil = list.get(4).getYear();
		String coal = list.get(5).getYear();

		ConsumMonitor consumMonitor1 = new ConsumMonitor();
		consumMonitor1.setId(1);
		consumMonitor1.setTotal(elec);
		consumMonitorService.update(consumMonitor1);

		ConsumMonitor consumMonitor2 = new ConsumMonitor();
		consumMonitor2.setId(2);
		consumMonitor2.setTotal(water);
		consumMonitorService.update(consumMonitor2);

		ConsumMonitor consumMonitor3 = new ConsumMonitor();
		consumMonitor3.setId(3);
		consumMonitor3.setTotal(natural);
		consumMonitorService.update(consumMonitor3);

		ConsumMonitor consumMonitor4 = new ConsumMonitor();
		consumMonitor4.setId(4);
		consumMonitor4.setTotal(gas);
		consumMonitorService.update(consumMonitor4);

		ConsumMonitor consumMonitor5 = new ConsumMonitor();
		consumMonitor5.setId(5);
		consumMonitor5.setTotal(oil);
		consumMonitorService.update(consumMonitor5);

		ConsumMonitor consumMonitor6 = new ConsumMonitor();
		consumMonitor6.setId(6);
		consumMonitor6.setTotal(coal);
		consumMonitorService.update(consumMonitor6);

		setConsumActual();
	}

	public void setConsumActual() {
		List<EnergyYear> list = energyYearService.selectAll();
		String elec = list.get(0).getJan();
		String water = list.get(1).getJan();
		String natural = list.get(2).getJan();
		String gas = list.get(3).getJan();
		String oil = list.get(4).getJan();
		String coal = list.get(5).getJan();

		ConsumMonitor consumMonitor1 = new ConsumMonitor();
		consumMonitor1.setId(1);
		consumMonitor1.setActual(elec);
		consumMonitorService.update(consumMonitor1);

		ConsumMonitor consumMonitor2 = new ConsumMonitor();
		consumMonitor2.setId(2);
		consumMonitor2.setActual(water);
		consumMonitorService.update(consumMonitor2);

		ConsumMonitor consumMonitor3 = new ConsumMonitor();
		consumMonitor3.setId(3);
		consumMonitor3.setActual(natural);
		consumMonitorService.update(consumMonitor3);

		ConsumMonitor consumMonitor4 = new ConsumMonitor();
		consumMonitor4.setId(4);
		consumMonitor4.setActual(gas);
		consumMonitorService.update(consumMonitor4);

		ConsumMonitor consumMonitor5 = new ConsumMonitor();
		consumMonitor5.setId(5);
		consumMonitor5.setActual(oil);
		consumMonitorService.update(consumMonitor5);

		ConsumMonitor consumMonitor6 = new ConsumMonitor();
		consumMonitor6.setId(6);
		consumMonitor6.setActual(coal);
		consumMonitorService.update(consumMonitor6);
	}

	@RequestMapping("/calculate.action")
	public void toCalculate(HttpServletResponse response) throws ServletException, IOException {
		count1();
		countYear();
		PrintWriter out = response.getWriter();
		out.print(1);

	}

	public void countYear() {
		// energyYear table setYear() amount
		List<EnergyYear> listCountYear = energyYearService.selectPeriods(1, 8);

		int yearCount = 0;
		for (int i = 0; i < 8; i++) {

			yearCount = Integer.parseInt(listCountYear.get(i).getJan())
					+ Integer.parseInt(listCountYear.get(0).getFeb()) + Integer.parseInt(listCountYear.get(i).getMar())
					+ Integer.parseInt(listCountYear.get(0).getApr()) + Integer.parseInt(listCountYear.get(i).getMay())
					+ Integer.parseInt(listCountYear.get(0).getJun()) + Integer.parseInt(listCountYear.get(i).getJuly())
					+ Integer.parseInt(listCountYear.get(0).getAug()) + Integer.parseInt(listCountYear.get(i).getSep())
					+ Integer.parseInt(listCountYear.get(0).getOct()) + Integer.parseInt(listCountYear.get(i).getNov())
					+ Integer.parseInt(listCountYear.get(0).getDec1());

			EnergyYear energyYear = new EnergyYear();
			energyYear.setId(i + 1);
			energyYear.setYear(yearCount + "");
			energyYearService.update(energyYear);
		}

	}

	public void count1() {
		// energyYear table id=7 count in
		List<EnergyYear> listCount = energyYearService.selectPeriods(1, 6);
		int sumJan = 0;
		int sumFeb = 0;
		int sumMar = 0;
		int sumApr = 0;
		int sumMay = 0;
		int sumJune = 0;
		int sumJuly = 0;
		int sumAug = 0;
		int sumSep = 0;
		int sumOct = 0;
		int sumNov = 0;
		int sumDec = 0;
		int sumYear = 0;
		for (EnergyYear energyYear : listCount) {
			sumJan = sumJan + Integer.parseInt(energyYear.getJan());
			sumFeb = sumFeb + Integer.parseInt(energyYear.getFeb());
			sumMar = sumMar + Integer.parseInt(energyYear.getMar());
			sumApr = sumApr + Integer.parseInt(energyYear.getApr());
			sumMay = sumMay + Integer.parseInt(energyYear.getMay());
			sumJune = sumJune + Integer.parseInt(energyYear.getJun());
			sumJuly = sumJuly + Integer.parseInt(energyYear.getJuly());
			sumAug = sumAug + Integer.parseInt(energyYear.getAug());
			sumSep = sumSep + Integer.parseInt(energyYear.getSep());
			sumOct = sumOct + Integer.parseInt(energyYear.getOct());
			sumNov = sumNov + Integer.parseInt(energyYear.getNov());
			sumDec = sumDec + Integer.parseInt(energyYear.getDec1());
			sumYear = sumYear + Integer.parseInt(energyYear.getYear());
		}
		EnergyYear energyYear1 = new EnergyYear();
		energyYear1.setId(7);
		energyYear1.setJan(sumJan + "");
		energyYear1.setFeb(sumFeb + "");
		energyYear1.setMar(sumMar + "");
		energyYear1.setApr(sumApr + "");
		energyYear1.setMay(sumMay + "");
		energyYear1.setJun(sumJune + "");
		energyYear1.setJuly(sumJuly + "");
		energyYear1.setAug(sumAug + "");
		energyYear1.setSep(sumSep + "");
		energyYear1.setOct(sumOct + "");
		energyYear1.setNov(sumNov + "");
		energyYear1.setDec1(sumDec + "");
		energyYear1.setYear(sumYear + "");
		energyYearService.update(energyYear1);

		Random rd = new Random();

		EnergyYear energyYear2 = new EnergyYear();
		energyYear2.setId(8);
		energyYear2.setJan(sumJan + rd.nextInt(1000) + "");
		energyYear2.setFeb(sumFeb + rd.nextInt(1000) + "");
		energyYear2.setMar(sumMar + rd.nextInt(1000) + "");
		energyYear2.setApr(sumApr + rd.nextInt(1000) + "");
		energyYear2.setMay(sumMay + rd.nextInt(1000) + "");
		energyYear2.setJun(sumJune + rd.nextInt(1000) + "");
		energyYear2.setJuly(sumJuly + rd.nextInt(1000) + "");
		energyYear2.setAug(sumAug + rd.nextInt(1000) + "");
		energyYear2.setSep(sumSep + rd.nextInt(1000) + "");
		energyYear2.setOct(sumOct + rd.nextInt(1000) + "");
		energyYear2.setNov(sumNov + rd.nextInt(1000) + "");
		energyYear2.setDec1(sumDec + rd.nextInt(1000) + "");
		energyYear2.setYear(sumYear + rd.nextInt(1000) + "");
		energyYearService.update(energyYear2);
	}

	@RequestMapping("/getSelect.action")
	public void getSelect(@RequestParam("select") String select1, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		select = "";
		setSelect(select1);
		out.print(223);
	}

	@RequestMapping("/insert.action")
	public void insert() {
		EnergyYear energyYear = new EnergyYear();
		for (int i = 1; i < 13; i++) {
			energyYear.setName("水");
			energyYearService.insert(energyYear);
		}
	}

	@RequestMapping("/updateThis.action")
	public ModelAndView updateThis(@RequestParam("id") Integer id, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		model.addObject("id", id);
		EnergyYear energyYear = energyYearService.selectById(id);
		model.addObject("name", energyYear.getName());
		model.addObject("jan", energyYear.getJan());
		model.addObject("feb", energyYear.getFeb());
		model.addObject("mar", energyYear.getMar());
		model.addObject("apr", energyYear.getApr());
		model.addObject("may", energyYear.getMay());
		model.addObject("jun", energyYear.getJun());
		model.addObject("july", energyYear.getJuly());
		model.addObject("aug", energyYear.getAug());
		model.addObject("sep", energyYear.getSep());
		model.addObject("oct", energyYear.getOct());
		model.addObject("nov", energyYear.getNov());
		model.addObject("dec", energyYear.getDec1());
		model.addObject("year", energyYear.getYear());
		model.setViewName("energyYear_update");
		return model;
	}

	@RequestMapping("/setList.action")
	public void setList(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		energyYearList = energyYearService.selectAll();
		setEnergyYearList(energyYearList);
		PrintWriter out = response.getWriter();
		out.print(7);
	}

	@RequestMapping("/getList.action")
	public ModelAndView getList(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		energyYearList = energyYearService.selectAll();
		model.addObject("energyYearList", energyYearList);
		model.setViewName("details");
		return model;
	}

	@RequestMapping("/showList.action")
	public ModelAndView showList(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		energyYearList = energyYearService.selectAll();
		select = getSelect();
		model.addObject("option", select);
		model.addObject("energyYearList", energyYearList);
		model.setViewName("database");
		return model;

	}

	@RequestMapping(value = "updateSelective.action", method = { RequestMethod.POST })
	public ModelAndView updateSelective(HttpServletRequest request, HttpSession session) throws IOException {
		String id1 = request.getParameter("id");
		int id = Integer.parseInt(id1.trim());
		String name = request.getParameter("name");
		String jan = request.getParameter("jan");
		String feb = request.getParameter("feb");
		String mar = request.getParameter("mar");
		String apr = request.getParameter("apr");
		String may = request.getParameter("may");
		String jun = request.getParameter("jun");
		String july = request.getParameter("july");
		String aug = request.getParameter("aug");
		String sep = request.getParameter("sep");
		String oct = request.getParameter("oct");
		String nov = request.getParameter("nov");
		String dec1 = request.getParameter("dec1");
		String year = request.getParameter("year");

		EnergyYear energyYear = new EnergyYear();
		energyYear.setId(id);
		energyYear.setName(name);
		energyYear.setJan(jan);
		energyYear.setFeb(feb);
		energyYear.setMar(mar);
		energyYear.setApr(apr);
		energyYear.setMay(may);
		energyYear.setJun(jun);
		energyYear.setJuly(july);
		energyYear.setAug(aug);
		energyYear.setSep(sep);
		energyYear.setOct(oct);
		energyYear.setNov(nov);
		energyYear.setDec1(dec1);
		energyYear.setYear(year);

		energyYearService.update(energyYear);

		ModelAndView model = new ModelAndView();

		energyYearList = getEnergyYearList();
		model.addObject("energyYearList", energyYearList);
		model.setViewName("database");
		return model;
	}
}
